package com.welsy.user.command
{
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.welsy.user.business.*;
	import com.welsy.user.event.GetCourseTrackEvent;
	import com.welsy.util.Comparator;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.controls.Alert;
	import mx.collections.ICollectionView;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.utils.ArrayUtil;
	import com.welsy.user.model.UserModelLocator;
	import com.welsy.authoring.vo.*;
	import com.welsy.authoring.vo.course.*;
	import com.welsy.authoring.model.CourseModelLocator;
		
	
	/**
	 * @version	$Revision: 1.1 $
	 */
	public class GetCourseTrackCommand implements ICommand, IResponder
	{
	  	public function GetCourseTrackCommand()
		{	 
		}
	
		public function execute( event : CairngormEvent ): void
		{
			var getCourseEvent:GetCourseTrackEvent = GetCourseTrackEvent( event );
			
			var delegate : GetCourseTrackDelegate = new GetCourseTrackDelegate( this );
			delegate.getCourseComponents(getCourseEvent.courseID,getCourseEvent.userID);			    			
		}
	
		public function result( event : Object ) : void
		{				
			trace("GetCourseComponents_onResult");
			var ccXML:XMLList = event.result as XMLList;
			var courseComponentsVO:CourseComponentsVO = new CourseComponentsVO;
			courseComponentsVO = parseXMLCourseComponents(ccXML);
			
			//var _cc : CourseComponentsVO = CourseComponentsVO( event );
			var model : UserModelLocator = UserModelLocator.getInstance();
			var courseModel : CourseModelLocator = CourseModelLocator.getInstance();
			
			courseModel.selectedCourse = courseComponentsVO;   		
			model.selectedCourse = courseComponentsVO;				
			
			trace("TITOLO COMMAND: " + courseModel.selectedCourse.course.title);
		}

		private function parseXMLCourseComponents(cclist:XMLList):CourseComponentsVO
		{			
			var cc:CourseComponentsVO = new CourseComponentsVO;
				
			//get course properties			
			var courseVO:CourseVO = new CourseVO;
			var course:XML = XML(cclist.child(0).child(0));
			courseVO.setProperties(course);			
			trace(courseVO.toString());
			//set coursecomponents obj
			cc.course = courseVO; 
							
			//get coursecomponents properties					
			var item:XML;		
			var list:XMLList =  cclist.descendants(CourseQNames.qnModules).children();
									
	            for each(item in list) {
	            	trace("add module");
	            	var moduleXmlList:XMLList = item.child(CourseQNames.qnModule);
	            		            		            		
	            	var module:ModuleVO = new ModuleVO();	            		            				            
	            	module.setProperties(moduleXmlList);
	                
	                //add elements
	                var elementsXmlList:XMLList = item.child(CourseQNames.qnElements).children();
	                for each(item in elementsXmlList) {
	            		trace("add element");	            		       		            		
	            		var element:ElementVO = new ElementVO();	            		            				            
	            		element.setProperties(item);
	            		element.setParent(module)
	                
	                	//add element to the module obj
	                	module.addItem(element); 
			    	}
			    	
			    	//add module to the coursecomponents obj
	                cc.addItem(module);	  		            
			    }
			
			return cc;
		}
	
		public function fault( event : Object ) : void
		{
			var faultEvent : FaultEvent = FaultEvent( event );
			Alert.show( "Si è verificato un errore" );
		}
	}

}